<?php

class UserGroupController extends GeanyController {

    // <editor-fold defaultstate="collapsed" desc="STATIC & PRIVATE VARIABLE DECLARATION">
    private $validator;
    const _FUNCTION_ID = 'usergroup';
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="CONSTRUCTOR">
  
    public function __construct(){
        parent::__construct();
        $this->loadDefaultValue();
        $this->loadServices();
        $this->validator = null;
    }
	
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="ACTION-HANDLER">
  
    public function index(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        $data['Model'] = $this->mService->getList();
        return View::make('usergroup/index', $data);
    }

    public function create(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }

        $userGroup = new UserGroup();
        
        try {
            if(Input::get('submit')==null){
                return $this->createInputView($userGroup);
            }
            else{
                $userGroup = $this->bindData();
                if($this->validateInput(Input::all())==FALSE) return $this->createInputView($userGroup);
                $newId = $this->mService->insertObject($userGroup);
                
                if($newId!=null){
                    Return Redirect::to('usergroup/detail/'.$newId);
                }
                return $this->createInputView($userGroup);
            }
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return $this->createInputView($this->bindData());
        }

    }
    
    public function detail($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        try {
            $result = $this->mService->getObject($id);
            if($result==null) {
                PageData::addModelError("Group with Id [ ".$id." ] Not Found ");
                Return Redirect::to('usergroup')->with('ModelState',PageData::getModelState());
            }
            $data['Model'] = $result;
            return View::make('usergroup/detail', $data);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            Return Redirect::to('usergroup');
        }
    }
    
    public function delete($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }

        try {
            $this->mService->deleteObject($id);
            
            Return Redirect::to('usergroup');
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            Return Redirect::to('usergroup');
        }
    }

    public function edit($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        $userGroup = new UserGroup();

        try {
            if(Input::get('submit')==null){
                $userGroup = $this->mService->getObject($id);
                return $this->createInputView($userGroup);
            }
            else{
                $userGroup = $this->bindData();
                if($this->validateInput(Input::all())==FALSE) return $this->createInputView($userGroup);
                if($this->mService->updateObject($userGroup,$id)==true){
                    Return Redirect::to('usergroup/detail/'.$userGroup->getId());
                }
                return $this->createInputView($userGroup);
            }
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return $this->createInputView($this->bindData());
        }
        
    }

    
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="PRIVATE-FUNCTION">
    
    private function loadComboIsAllowAccess($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $arr_data[true] = 'TRUE';
        $arr_data[false] = 'FALSE';
        
        $this->data['IsAllowAccessCombo'] = $arr_data;
    }
    
    private function loadComboIsEnabled($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $arr_data[true] = 'TRUE';
        $arr_data[false] = 'FALSE';
        
        $this->data['IsEnabledCombo'] = $arr_data;
    }
    
    private function loadComboModule($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $moduleService = new ModuleService();
        $modules = $moduleService->getList();
        foreach ($modules as $item){
            $arr_data[$item->getId()] = $item->getName();
        }
        
        $this->data['ModulesCombo'] = $arr_data;
    }
    
    private function loadComboFunctions($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $functionsService = new FunctionsService();
        $functions = $functionsService->getList();
        foreach ($functions as $item){
            $arr_data[$item->getId()] = $item->getName();
        }
        
        $this->data['FunctionsCombo'] = $arr_data;
    }
    
    private function createInputView($userGroup=null){
        $this->loadComboModule();
        $this->loadComboFunctions();
        $this->loadComboIsEnabled();
        $this->loadComboIsAllowAccess();
        $this->data['Model'] = $userGroup;
        return View::make('usergroup/input', $this->data);
    }
    
    private function bindData(){
        $data = Input::all();

        $userGroup = New UserGroup();
        $userGroup->setName($data["name"]);
        $userGroup->setDescription($data["description"]);
        $userGroup->setIsEnabled($data["is_enabled"]);
        $privilegeInfos = [];
        $privilegeInfoIds = Input::get('privilegeInfo', array());
        for($i=0; $i<count($privilegeInfoIds); $i++) {
            $privilegeInfo = new PrivilegeInfo();
            $privilegeInfo->setId($data["privilegeInfoId"][$i]);
            
            $Group = New UserGroup();
            $Group->setId($userGroup->getId());
            $Group->setIsLoaded(true);
            $privilegeInfo->setGroup($Group);
            
            $functions = new Functions();
            $functions->setId($data["functionId"][$i]);
            $functions->setIsLoaded(false);
            $privilegeInfo->setFunctions($functions);
            
            $module = new Module();
            if($privilegeInfo->getFunctions()!=null && $privilegeInfo->getFunctions()->getModule()!=null)
            $module->setId($privilegeInfo->getFunctions()->getModule()->getId());
            $module->setIsLoaded(true);
            $privilegeInfo->setModule($module);
            
            $privilegeInfo->setIsAllowAccess($data["isAllowAccess"][$i]);
            $privilegeInfo->setIsLoaded(true);
            
            $privilegeInfos[] = $privilegeInfo;
        }
        $userGroup->setPrivilegeInfo($privilegeInfos);
        
        return $userGroup;
    }
 
    private function validateInput($data){
        
        if($data==null) return false;
        $rules = array(
            'name' => 'required'
        );

        $messages = array(
            'name' => 'Group Name is Required.'
        );
        
        $this->validator = Validator::make($data, $rules, $messages);
        if($this->validator->fails()) PageData::assignValidatorToModelState ($this->validator);
        return $this->validator->passes();
    }
    
    private function loadDefaultValue(){
        $this->setFunctionId(self::_FUNCTION_ID);
        $this->page_data['_MODULE_NAME'] = $this->getFunctionName();
    }

    private function loadServices(){
        $this->mService = new UserGroupService();
    }
    
    // </editor-fold>
    
    
}
?>